package com.cn.bukeManage.service.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.cn.bukeManage.dao.UserDao;
import com.cn.bukeManage.pojo.Role;
import com.cn.bukeManage.pojo.User;
import com.cn.bukeManage.pojo.page.Page;
import com.cn.bukeManage.service.UserService;
import com.cn.bukeManage.utils.MD5Utils.MD5Utils;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource
	private UserDao userDao;
	
	
	@Override
	public User get( Integer userId){
		return userDao.getUserById(userId);
	}

	@Override
	public User checkLogin(User user) {
		return userDao.getUserByUser(user);
	}
	
	
	@Override
	public User checkUser(String userName,String password){
		User user=getUser(userName);
		if(user !=null){
			//密码
			String salt = user.getWeiyi();
			String saltPassword = MD5Utils.GetMD5Code(String.valueOf(password)+ salt);
			if(saltPassword.equals(user.getPasswd())){
				return user;
			}
		}
		return null;
	}
	
	
	@Override
	public Double getMyWallet(String userId){
		return userDao.selectMyWallet(userId);
	}
	
	

	@Override
	public int isUserExit(User user) {
		return userDao.isUserExit(user);
	}

	@Override
	public void updateUser(User user) {
		userDao.updateUser(user);
	}

	@Override
	public User getUser(String userName) {
		// TODO Auto-generated method stub
		return userDao.selectUserByName(userName);
	}

	@Override
	public Set<Role> getRoles(Integer userId) {
		// TODO Auto-generated method stub
		return userDao.selectRoles(userId);
	}

	@Override
	public Set<com.cn.bukeManage.pojo.Resource> getResources(Integer userId) {
		// TODO Auto-generated method stub
		return userDao.selectResources(userId);
	}

	@Override
	public Integer blockUp(Integer userId) {
		// TODO Auto-generated method stub
		return userDao.blockUp(userId);
	}

	@Override
	public Page findAllUsers(Integer currentPage,Model model) {
		Page page=new Page();
		page.setRows(userDao.selectUsersCount());
		page.setCurrentPage(currentPage);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("page", page.getBegin());
		map.put("pageSize", page.getPageSize());
		List<Map<String, Object>> list = userDao.selectAllUsers(map);
		for(Map<String, Object>m:list){
			for(String key : m.keySet()){
				System.out.println(key+""+m.get(key));
			}
		}
		model.addAttribute("user",list);
		return page;
	}

	

	
}
